<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\users;

use think\facade\Cache;
use app\common\dao\users\UsersDao;
use app\common\services\JwtService;
use app\common\repositories\BaseRepository;

/**
 * Class UsersRepository
 *
 * @mixin UsersDao
 */
class UsersRepository extends BaseRepository
{

    public function __construct(UsersDao $dao)
    {
        $this->dao = $dao;
    }


    /**
     *密码加密
     * @param $password
     * @return string
     *
     * @author Amos
     * 2024-01-07 08:50
     */
    public function passwordEncrypt($password)
    {
        return password_hash($password, PASSWORD_BCRYPT);
    }


    /**
     * 密码验证
     * @param $password
     * @param $userPassword // 用户密码
     * @return bool
     *
     * @author Amos
     * 2024-01-07 08:51
     */
    public function passwordVerify($password, $userPassword)
    {
        return password_verify($password, $userPassword);
    }

    /**
     * 注册账号
     * @param $data
     * @return \app\common\dao\BaseDao|\think\Model
     *
     * @author Amos
     * 2024-01-19 17:15
     */
    public function register($data)
    {
        if (empty($data['nickname']) && isset($data['username'])) {
            $data['nickname'] = $data['username'];
        } else if (empty($data['nickname']) && isset($data['mobile'])) {
            $data['nickname'] = substr_replace($data['mobile'], '****', 3, 4);
        }
        if (empty($data['password']) && isset($data['mobile'])) {
            $data['password'] = substr($data['mobile'], -6);
        }
        return $this->create($data);
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return $this->update($info['id'], $data);
    }

    public function propertyDditInfo(int $id, array $data)
    {
        return $this->update($id, $data);
    }

    public function update(int $id, array $data)
    {
        if (isset($data['password']) && strlen($data['password']) !== 60) {
            if ($data['password'] !== '') {
                $data['password'] = $this->passwordEncrypt($data['password']);
            } else {
                unset($data['password']);
            }
        }
        return $this->dao->update($id, $data);
    }


    /**
     * 创建token
     * @param $userInfo
     * @return string
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     * @author Amos
     * 2024-01-24 21:19
     */
    public function createToken($userInfo): string
    {
        $service = new JwtService();
        $res = $service->createToken($userInfo['id'], 'user', strtotime('+15 day'));
        $token = sha1($res['token']);
        Cache::store('usertoken')->set('user_api_' . $token, $res['token'], 86400);
        return $token;
    }

    /**
     * 更新token
     * @param string $token
     * @return void
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     * @author Amos
     * 2024-01-24 21:27
     */
    public function updateToken(string $token)
    {
        Cache::store('file')->set('user_api_' . $token, $this->getToken(), 86400);
    }


    /**
     * 生成token
     * @param $token
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     * @author Amos
     * 2024-01-24 21:28
     */
    public function getToken($token = '')
    {
        if ($token) {
            $token = 'user_api_' . $token;
        } else {
            $token = 'user_api_' . app('request')->header('Token');
        }

        return Cache::store('usertoken')->get($token);
    }

    /**
     * 清除token
     * @return void
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     * @author Amos
     * 2024-01-24 21:28
     */
    public function clearToken()
    {
        Cache::store('user_api')->delete('user_api_' . app('request')->header('Token'));
    }

    /**
     * 过滤显示的用户信息
     * @param $userInfo
     * @return mixed
     *
     * @author Amos
     * 2024-01-24 21:28
     */
    public function showApiFilter($userInfo)
    {
        $data = $userInfo
            ->hidden([
                'password', 'session_id', 'groupInfo', 'cert_id', 'level_id', 'label_id', 'group_id'
            ]);
        return $data;
    }

}